package bdhub.littletail.lib.ui.widget.smiley;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.DynamicDrawableSpan;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import bdhub.littletail.R;
import bdhub.littletail.app.LittleTailApplication;
import bdhub.littletail.lib.ui.widget.DrawableSpan;

public final class EmojiSmileyParser {

	private static class SingletonContainer {
		public static EmojiSmileyParser instance = new EmojiSmileyParser(LittleTailApplication.sharedInstance());
	}

	public static EmojiSmileyParser getInstance() {
		return SingletonContainer.instance;
	}

	protected Pattern mPattern;

	protected HashMap<Integer, String> mResToSmiley;

	protected HashMap<String, Integer> mSmileyToRes ;

//	protected HashMap<String, Integer> mOldSmileyToRes ;

	private final static int ONLY_SEND_COUNT = 124;

	private final static int[] sIconIds = {
			R.drawable.emo1, R.drawable.emo2, R.drawable.emo3, R.drawable.emo4, R.drawable.emo5, R.drawable.emo6, R.drawable.emo7,
			R.drawable.emo8, R.drawable.emo9, R.drawable.emo10, R.drawable.emo11, R.drawable.emo12, R.drawable.emo13, R.drawable.emo14,
			R.drawable.emo15, R.drawable.emo16, R.drawable.emo17, R.drawable.emo18, R.drawable.emo19, R.drawable.emo20, R.drawable.emo21,

			R.drawable.emo22, R.drawable.emo23, R.drawable.emo24, R.drawable.emo25, R.drawable.emo26, R.drawable.emo27, R.drawable.emo28,
			R.drawable.emo29, R.drawable.emo30, R.drawable.emo31, R.drawable.emo32, R.drawable.emo33, R.drawable.emo34, R.drawable.emo35,
			R.drawable.emo36, R.drawable.emo37, R.drawable.emo38, R.drawable.emo39, R.drawable.emo40, R.drawable.emo41, R.drawable.emo42,

			R.drawable.emo43, R.drawable.emo44, R.drawable.emo45, R.drawable.emo46, R.drawable.emo47, R.drawable.emo48, R.drawable.emo49,
			R.drawable.emo50, R.drawable.emo51, R.drawable.emo52, R.drawable.emo53, R.drawable.emo54, R.drawable.emo55, R.drawable.emo56,
			R.drawable.emo57, R.drawable.emo58, R.drawable.emo59, R.drawable.emo60, R.drawable.emo61, R.drawable.emo62, R.drawable.emo63,

			R.drawable.emo64, R.drawable.emo65, R.drawable.emo66, R.drawable.emo67, R.drawable.emo68, R.drawable.emo69, R.drawable.emo70,
			R.drawable.emo71, R.drawable.emo72, R.drawable.emo73, R.drawable.emo74, R.drawable.emo75, R.drawable.emo76, R.drawable.emo77,
			R.drawable.emo78, R.drawable.emo79, R.drawable.emo80, R.drawable.emo81, R.drawable.emo82, R.drawable.emo83, R.drawable.emo84,

			R.drawable.emo85, R.drawable.emo86, R.drawable.emo87, R.drawable.emo88, R.drawable.emo89, R.drawable.emo90, R.drawable.emo91,
			R.drawable.emo92, R.drawable.emo93, R.drawable.emo94, R.drawable.emo95, R.drawable.emo96, R.drawable.emo97, R.drawable.emo98,
			R.drawable.emo99, R.drawable.emo100, R.drawable.emo101, R.drawable.emo102, R.drawable.emo103, R.drawable.emo104, R.drawable.emo105,

			R.drawable.emo106, R.drawable.emo107, R.drawable.emo108, R.drawable.emo109, R.drawable.emo110, R.drawable.emo111, R.drawable.emo112,
			R.drawable.emo113, R.drawable.emo114, R.drawable.emo115, R.drawable.emo116, R.drawable.emo117, R.drawable.emo118, R.drawable.emo119,
			R.drawable.emo120, R.drawable.emo121, R.drawable.emo122, R.drawable.emo123, R.drawable.emo124,

			//不发送，只解析
			R.drawable.u1f600, R.drawable.u1f617, R.drawable.u1f619, R.drawable.u1f61b, R.drawable.u1f605, R.drawable.u1f629, R.drawable.u1f62b, R.drawable.u1f624,
			R.drawable.u1f606, R.drawable.u1f60b, R.drawable.u1f60e, R.drawable.u1f634, R.drawable.u1f635, R.drawable.u1f61f, R.drawable.u1f626, R.drawable.u1f627,
			R.drawable.u1f608, R.drawable.u1f62e, R.drawable.u1f62c, R.drawable.u1f610, R.drawable.u1f615, R.drawable.u1f62f, R.drawable.u1f636, R.drawable.u1f607,
			R.drawable.u1f611, R.drawable.u1f472, R.drawable.u1f473, R.drawable.u1f46e, R.drawable.u1f477, R.drawable.u1f482, R.drawable.u1f478, R.drawable.u1f63a,
			R.drawable.u1f638, R.drawable.u1f63b, R.drawable.u1f63d, R.drawable.u1f63c, R.drawable.u1f640, R.drawable.u1f63f, R.drawable.u1f639, R.drawable.u1f63e,
			R.drawable.u1f479, R.drawable.u1f47a, R.drawable.u1f648, R.drawable.u1f649, R.drawable.u1f64a, R.drawable.u1f4ab, R.drawable.u1f4a5, R.drawable.u1f4a7,
			R.drawable.u1f445, R.drawable.u270a, R.drawable.u1f44b, R.drawable.u270b, R.drawable.u1f450, R.drawable.u1f446, R.drawable.u1f447, R.drawable.u1f449,
			R.drawable.u1f448, R.drawable.u1f64c, R.drawable.u261d, R.drawable.u1f6b6, R.drawable.u1f3c3, R.drawable.u1f483, R.drawable.u1f46b, R.drawable.u1f46a,
			R.drawable.u1f46c, R.drawable.u1f46d, R.drawable.u1f64b, R.drawable.u1f486, R.drawable.u1f487, R.drawable.u1f485, R.drawable.u1f470, R.drawable.u1f64e,
			R.drawable.u1f64d, R.drawable.u1f3a9, R.drawable.u1f451, R.drawable.u1f452, R.drawable.u1f45f, R.drawable.u1f45e, R.drawable.u1f461, R.drawable.u1f460,
			R.drawable.u1f462, R.drawable.u1f455, R.drawable.u1f454, R.drawable.u1f45a, R.drawable.u1f457, R.drawable.u1f3bd, R.drawable.u1f456, R.drawable.u1f458,
			R.drawable.u1f459, R.drawable.u1f4bc, R.drawable.u1f45c, R.drawable.u1f45d, R.drawable.u1f45b, R.drawable.u1f453, R.drawable.u1f380, R.drawable.u1f302,
			R.drawable.u1f484, R.drawable.u1f499, R.drawable.u1f49c, R.drawable.u1f49a, R.drawable.u1f497, R.drawable.u1f493, R.drawable.u1f495, R.drawable.u1f496,
			R.drawable.u1f49e, R.drawable.u1f48c, R.drawable.u1f48b, R.drawable.u1f48d, R.drawable.u1f464, R.drawable.u1f465, R.drawable.u1f4ac, R.drawable.u1f4ad
	};

	private final static String[] sSmileyTexts = {
			"\uD83D\uDE04", "\uD83D\uDE0A", "\uD83D\uDE03",       "\u263A", "\uD83D\uDE09", "\uD83D\uDE0D", "\uD83D\uDE18",
			"\uD83D\uDE1A", "\uD83D\uDE33", "\uD83D\uDE0C", "\uD83D\uDE01", "\uD83D\uDE1C", "\uD83D\uDE1D", "\uD83D\uDE12",
			"\uD83D\uDE0F", "\uD83D\uDE13", "\uD83D\uDE14", "\uD83D\uDE1E", "\uD83D\uDE16", "\uD83D\uDE25", "\uD83D\uDE30",

			"\uD83D\uDE28", "\uD83D\uDE23", "\uD83D\uDE22", "\uD83D\uDE2D", "\uD83D\uDE02", "\uD83D\uDE32", "\uD83D\uDE31",
			"\uD83D\uDE20", "\uD83D\uDE21", "\uD83D\uDE2A", "\uD83D\uDE37", "\uD83D\uDC7F", "\uD83D\uDC7D", "\uD83D\uDC9B",
			      "\u2764", "\uD83D\uDC94", "\uD83D\uDC98",       "\u2728", "\uD83C\uDF1F", "\uD83D\uDCA2",       "\u2757",

			      "\u2753",       "\u2B55",       "\u274C", "\uD83D\uDCA4", "\uD83D\uDCA8", "\uD83D\uDCA6", "\uD83C\uDFB6",
			"\uD83D\uDD25", "\uD83D\uDCA9", "\uD83D\uDC4D", "\uD83D\uDC4E", "\uD83D\uDC4C", "\uD83D\uDC4A",       "\u270C",
			"\uD83D\uDC4F", "\uD83D\uDE4F", "\uD83D\uDCAA", "\uD83D\uDC44", "\uD83D\uDC42", "\uD83D\uDC40", "\uD83D\uDC43",

			"\uD83D\uDC6F", "\uD83D\uDE46", "\uD83D\uDE45", "\uD83D\uDC81", "\uD83D\uDE47", "\uD83D\uDC8F", "\uD83D\uDC91",
			"\uD83D\uDC66", "\uD83D\uDC67", "\uD83D\uDC69", "\uD83D\uDC68", "\uD83D\uDC76", "\uD83D\uDC75", "\uD83D\uDC74",
			"\uD83D\uDC71", "\uD83D\uDC7C", "\uD83D\uDC80", "\uD83D\uDC63",       "\u2600",       "\u2614",       "\u2601",

			      "\u26C4", "\uD83C\uDF19",       "\u26A1", "\uD83C\uDF00", "\uD83D\uDC36", "\uD83D\uDC30", "\uD83D\uDC2F",
			"\uD83D\uDC3B", "\uD83D\uDC37", "\uD83D\uDC14", "\uD83D\uDC38", "\uD83D\uDC19", "\uD83D\uDC33", "\uD83D\uDC90",
			"\uD83C\uDF38", "\uD83C\uDF40", "\uD83C\uDF39", "\uD83C\uDF3B", "\uD83C\uDF43", "\uD83C\uDF84", "\uD83D\uDC7B",

			"\uD83D\uDC9D", "\uD83D\uDC8E",       "\u2615", "\uD83D\uDEAC", "\uD83D\uDCA1", "\uD83D\uDCB0", "\uD83C\uDF66",
			"\uD83C\uDF82", "\uD83C\uDF53", "\uD83C\uDF4E", "\uD83C\uDF4A", "\uD83C\uDF49",       "\u2668", "\uD83D\uDE80",
			"\uD83D\uDC92", "\uD83C\uDFE5", "\uD83D\uDC9F", "\uD83D\uDEB9", "\uD83D\uDEBA",

			//不发送，只解析
			"\uD83D\uDE00", "\uD83D\uDE17", "\uD83D\uDE19", "\uD83D\uDE1B", "\uD83D\uDE05", "\uD83D\uDE29", "\uD83D\uDE2B", "\uD83D\uDE24",
			"\uD83D\uDE06", "\uD83D\uDE0B", "\uD83D\uDE0E", "\uD83D\uDE34", "\uD83D\uDE35", "\uD83D\uDE1F", "\uD83D\uDE26", "\uD83D\uDE27",
			"\uD83D\uDE08", "\uD83D\uDE2E", "\uD83D\uDE2C", "\uD83D\uDE10", "\uD83D\uDE15", "\uD83D\uDE2F", "\uD83D\uDE36", "\uD83D\uDE07",
			"\uD83D\uDE11", "\uD83D\uDC72", "\uD83D\uDC73", "\uD83D\uDC6E", "\uD83D\uDC77", "\uD83D\uDC82", "\uD83D\uDC78", "\uD83D\uDE3A",
			"\uD83D\uDE38", "\uD83D\uDE3B", "\uD83D\uDE3D", "\uD83D\uDE3C", "\uD83D\uDE40", "\uD83D\uDE3F", "\uD83D\uDE39", "\uD83D\uDE3E",
			"\uD83D\uDC79", "\uD83D\uDC7A", "\uD83D\uDE48", "\uD83D\uDE49", "\uD83D\uDE4A", "\uD83D\uDCAB", "\uD83D\uDCA5", "\uD83D\uDCA7",
			"\uD83D\uDC45", "\u270A",       "\uD83D\uDC4B", "\u270B",       "\uD83D\uDC50", "\uD83D\uDC46", "\uD83D\uDC47", "\uD83D\uDC49",
			"\uD83D\uDC48", "\uD83D\uDE4C", "\u261D",       "\uD83D\uDEB6", "\uD83C\uDFC3", "\uD83D\uDC83", "\uD83D\uDC6B", "\uD83D\uDC6A",
			"\uD83D\uDC6C", "\uD83D\uDC6D", "\uD83D\uDE4B", "\uD83D\uDC86", "\uD83D\uDC87", "\uD83D\uDC85", "\uD83D\uDC70", "\uD83D\uDE4E",
			"\uD83D\uDE4D", "\uD83C\uDFA9", "\uD83D\uDC51", "\uD83D\uDC52", "\uD83D\uDC5F", "\uD83D\uDC5E", "\uD83D\uDC61", "\uD83D\uDC60",
			"\uD83D\uDC62", "\uD83D\uDC55", "\uD83D\uDC54", "\uD83D\uDC5A", "\uD83D\uDC57", "\uD83C\uDFBD", "\uD83D\uDC56", "\uD83D\uDC58",
			"\uD83D\uDC59", "\uD83D\uDCBC", "\uD83D\uDC5C", "\uD83D\uDC5D", "\uD83D\uDC5B", "\uD83D\uDC53", "\uD83C\uDF80", "\uD83C\uDF02",
			"\uD83D\uDC84", "\uD83D\uDC99", "\uD83D\uDC9C", "\uD83D\uDC9A", "\uD83D\uDC97", "\uD83D\uDC93", "\uD83D\uDC95", "\uD83D\uDC96",
			"\uD83D\uDC9E", "\uD83D\uDC8C", "\uD83D\uDC8B", "\uD83D\uDC8D", "\uD83D\uDC64", "\uD83D\uDC65", "\uD83D\uDCAC", "\uD83D\uDCAD"
	};

//	private final static String[] sOldSmileyTexts = {
//			"\ue415", "\ue056", "\ue057", "\ue414", "\ue405", "\ue106", "\ue418",
//			"\ue417", "\ue40d", "\ue40a", "\ue404", "\ue105", "\ue409", "\ue40e",
//			"\ue402", "\ue108", "\ue403", "\ue058", "\ue407", "\ue401", "\ue40f",
//
//			"\ue40b", "\ue406", "\ue413", "\ue411", "\ue412", "\ue410", "\ue107",
//			"\ue059", "\ue416", "\ue408", "\ue40c", "\ue11a", "\ue10c", "\ue32c",
//			"\ue022", "\ue023", "\ue329", "\ue32e", "\ue335", "\ue334", "\ue021",
//
//			"\ue020", "\ue332", "\ue333", "\ue13c", "\ue330", "\ue331", "\ue326",
//			"\ue11d", "\ue05a", "\ue00e", "\ue421", "\ue420", "\ue00d", "\ue011",
//			"\ue41f", "\ue41d", "\ue14c", "\ue41c", "\ue41b", "\ue419", "\ue41a",
//
//			"\ue429", "\ue424", "\ue423", "\ue253", "\ue426", "\ue111", "\ue425",
//			"\ue001", "\ue002", "\ue005", "\ue004", "\ue51a", "\ue519", "\ue518",
//			"\ue515", "\ue04e", "\ue11c", "\ue536", "\ue04a", "\ue04b", "\ue049",
//
//			"\ue048", "\ue04c", "\ue13d", "\ue443", "\ue052", "\ue52c", "\ue050",
//			"\ue051", "\ue10b", "\ue52e", "\ue531", "\ue10a", "\ue054", "\ue306",
//			"\ue030", "\ue110", "\ue032", "\ue305", "\ue447", "\ue033", "\ue11b",
//
//			"\ue437", "\ue035", "\ue045", "\ue30e", "\ue10f", "\ue12f", "\ue33a",
//			"\ue34b", "\ue347", "\ue345", "\ue346", "\ue348", "\ue123", "\ue10d",
//			"\ue43d", "\ue155", "\ue204", "\ue138", "\ue139",
//
//			//不发送，只解析
//			"",  "",  "",  "",  "",  "",  "",  "",
//			"",  "",  "",  "",  "",  "",  "",  "",
//			"",  "",  "",  "",  "",  "",  "",  "",
//			"",  "\uE516",  "\uE517",  "\uE152",  "\uE51B",  "\uE51E",  "\uE51C",  "",
//			"",  "",  "",  "",  "",  "",  "",  "",
//			"",  "",  "",  "",  "",  "",  "",  "",
//			"",  "\uE010",  "\uE41E",  "\uE012",  "\uE422",  "\uE22E",  "\uE22F",  "\uE231",
//			"\uE230", "\uE427", "\uE00F", "\uE201", "\uE115", "\uE51F", "\uE428",  "",
//			"",  "",  "", "\uE31E", "\uE31F", "\uE31D",  "",  "",
//			"", "\uE503", "\uE10E", "\uE318", "\uE007",  "", "\uE31A", "\uE13E",
//			"\uE31B", "\uE006", "\uE302",  "", "\uE319",  "",  "", "\uE321",
//			"\uE322", "\uE11E", "\uE323",  "",  "",  "", "\uE314", "\uE43C",
//			"\uE31C", "\uE32A", "\uE32D", "\uE32B", "\uE328", "\uE327",  "",  "",
//			"",  "", "\uE003", "\uE034",  "",  "",  "",  ""
//	};

    private final static String[] sFaceTexts = {
            "[微笑]", "[呲牙]",  "[坏笑]",   "[石化]",    "[发怒]",
            "[色]",  "[衰]",    "[泪汪汪]",    "[转身]", "[调皮]",
            "[糗大了]",    "[大哭]", "[内流满面]",    "[崇拜]", "[不屑]",
            "[累趴]", "[委屈]",  "[幸福]",   "[惊讶]",    "[沮丧]",
            "[狂汗]", "[慌张]",  "[震惊]",   "[绝望]",    "[抓狂]",
            "[可怜]", "[亲亲]",  "[惊恐]",   "[点赞]",    "[冷汗]",
            "[呆笑]", "[偷笑]",  "[晕]",    "[太委屈]",    "[脸红]",
            "[挖鼻屎]",    "[竖中指]",    "[祈求]", "[偷看]",  "[猪猪]",
            "[生气]", "[泪奔]",  "[气炸]",   "[么么哒]",   "[奸笑]",
            "[爱钱]", "[吃撑了]", "[鼓掌]",  "[求抱抱]",  "[病了]",
            "[敲打]", "[嘘]",   "[白眼]",    "[鄙视]", "[再见]",
            "[蛋糕]", "[啤酒]",  "[月亮]",   "[爱心]",    "[心动]",
            "[心碎]", "[音乐]",  "[玫瑰]",   "[喝茶]",    "[便便]",
            "[刀]",  "[吃饭]",   "[寿司]",    "[哨子]", "[黄牌]",
            "[红牌]", "[足球]",  "[篮球]",   "[脚印]"
    };

    public final static int[] sFaceIds = {
            R.drawable.face_1,  R.drawable.face_2,    R.drawable.face_3,  R.drawable.face_4,    R.drawable.face_5,
            R.drawable.face_6,  R.drawable.face_7,    R.drawable.face_8,  R.drawable.face_9,    R.drawable.face_10,
            R.drawable.face_11, R.drawable.face_12,  R.drawable.face_13,   R.drawable.face_14,    R.drawable.face_15,
            R.drawable.face_16, R.drawable.face_17,  R.drawable.face_18,   R.drawable.face_19,    R.drawable.face_20,
            R.drawable.face_21, R.drawable.face_22,  R.drawable.face_23,   R.drawable.face_24,    R.drawable.face_25,
            R.drawable.face_26, R.drawable.face_27,  R.drawable.face_28,   R.drawable.face_29,    R.drawable.face_30,
            R.drawable.face_31, R.drawable.face_32,  R.drawable.face_33,   R.drawable.face_34,    R.drawable.face_35,
            R.drawable.face_36, R.drawable.face_37,  R.drawable.face_38,   R.drawable.face_39,    R.drawable.face_40,
            R.drawable.face_41, R.drawable.face_42,  R.drawable.face_43,   R.drawable.face_44,    R.drawable.face_45,
            R.drawable.face_46, R.drawable.face_47,  R.drawable.face_48,   R.drawable.face_49,    R.drawable.face_50,
            R.drawable.face_51, R.drawable.face_52,  R.drawable.face_53,   R.drawable.face_54,    R.drawable.face_55,
            R.drawable.face_56, R.drawable.face_57,  R.drawable.face_58,   R.drawable.face_59,    R.drawable.face_60,
            R.drawable.face_61, R.drawable.face_62,  R.drawable.face_63,   R.drawable.face_64,    R.drawable.face_65,
            R.drawable.face_66, R.drawable.face_67,  R.drawable.face_68,   R.drawable.face_69,    R.drawable.face_70,
            R.drawable.face_71, R.drawable.face_72,  R.drawable.face_73,   R.drawable.face_74
    };

	private EmojiSmileyParser(Context context) {
		buildSmileyToRes();
		buildPattern();
	}

    /**
     * 对文本添加spans。可以指定表情图片的大小，如果传入的width<=0或者height<=0，则使用表情原始尺寸。
     *
     * @param builder
     *            可能含有表情的文本
     * @param width
     *            span的宽,如果小于0则使用图片本身默认值
     * @param height
     *            span的高,如果小于0则使用图片本身默认值
     * @param verticalAlignment
     *            one of {@link DynamicDrawableSpan#ALIGN_BOTTOM} or
     *            {@link DynamicDrawableSpan#ALIGN_BASELINE}.
     * @return 是否找到表情
     */
	public boolean addSmileySpans(SpannableStringBuilder builder, int width, int height, int verticalAlignment) {
		if (TextUtils.isEmpty(builder)) {
			return false;
		}

		boolean found = false;
		Matcher matcher = mPattern.matcher(builder);
		while (matcher.find()) {
			String match_str = matcher.group();
			Integer resId = mSmileyToRes.get(match_str);
//			if(null == resId){
//				resId = mOldSmileyToRes.get(match_str);
				if(null == resId){
					continue;
				}
//			}

			Drawable drawable = LittleTailApplication.sharedInstance().getResources().getDrawable(resId);
			if (width <= 0) {
				width = drawable.getIntrinsicWidth();
			}
			if (height <= 0) {
				height = drawable.getIntrinsicHeight();
			}
			drawable.setBounds(0, 0, width, height);
			builder.setSpan(new DrawableSpan(drawable, verticalAlignment), matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			found = true;
		}

		return found;
	}

	/**
	 * 检查字符串某个子断是否有表情符
	 *
	 * @param text
	 *            输入原串
	 * @param result
	 *            处理完后变成插入表情后的串
	 * @param start
	 *            子串开始
	 * @param end
	 *            子串的结束
	 * @param verticalAlignment
	 *            one of {@link DynamicDrawableSpan#ALIGN_BOTTOM} or
	 *            {@link DynamicDrawableSpan#ALIGN_BASELINE}.
	 * @return 发现多少个字符
	 */
	public int addSmileySpans(CharSequence text, SpannableStringBuilder result, int start, int end, int width, int height, int verticalAlignment) {
		if (TextUtils.isEmpty(text)) {
			return 0;
		}

		SpannableStringBuilder builder = new SpannableStringBuilder(text);
		CharSequence subText = text.subSequence(start, end);
		Matcher matcher = mPattern.matcher(subText);
		int size = 0;
		while (matcher.find()) {
			String match_str = matcher.group();
			Integer resId = mSmileyToRes.get(match_str);
//			if(null == resId){
//				resId = mOldSmileyToRes.get(match_str);
				if(null == resId){
					continue;
				}
//			}

			size++;
			Drawable drawable = LittleTailApplication.sharedInstance().getResources().getDrawable(resId);
			if (width <= 0) {
				width = drawable.getIntrinsicWidth();
			}
			if (height <= 0) {
				height = drawable.getIntrinsicHeight();
			}

			drawable.setBounds(0, 0, width, height);
			builder.setSpan(new DrawableSpan(drawable, verticalAlignment), start + matcher.start(), start + matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
		result.append(builder);
		return size;
	}

	//直接这样无法匹配
	//private static final String PatternString="([\uE000-\uF8FF]|\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDFFF])";
	protected void buildPattern() {
		StringBuilder sb = new StringBuilder();
		for(int i=0;i<sSmileyTexts.length;i++){
			sb.append(sSmileyTexts[i] + "|");
		}

		for(int i=0;i<sFaceTexts.length;i++){
			String temp = sFaceTexts[i];
			if(temp.length() > 0){
				sb.append(temp + "|");
			}
		}

		String temp = sb.toString();
		if(temp.length() > 0){
			temp = temp.substring(0, temp.length() - 1);
            temp = temp.replace("[", "\\[");
            temp = temp.replace("]", "\\]");
		}

		mPattern =  Pattern.compile(temp);
	}

	//
	// 返回替换后的结果
	public String replace(String src , String replace) {
		Matcher matcher = mPattern.matcher(src);
		if (matcher.find()) {
			return matcher.replaceAll(replace);
		} else {
			return src;
		}
	}

	/***
	 * Builds the hashtable we use for mapping the string version of a smiley
	 * (e.g. ":-)") to a resource ID for the icon version.
	 */
	protected void buildSmileyToRes() {
		if (sIconIds.length != sSmileyTexts.length ||
                sFaceIds.length != sFaceTexts.length) {
			// Throw an exception if someone updated DEFAULT_SMILEY_RES_IDS
			// and failed to update arrays.xml
			throw new IllegalStateException("Smiley resource ID/text mismatch");
		}

		HashMap<String, Integer> smileyToRes = new HashMap<String, Integer>(sFaceTexts.length + sSmileyTexts.length);
		HashMap<Integer, String> resToSmiley = new HashMap<Integer, String>(sFaceTexts.length + sSmileyTexts.length);
		for (int i = 0; i < sFaceTexts.length; i++) {
			smileyToRes.put(sFaceTexts[i], sFaceIds[i]);
			resToSmiley.put(sFaceIds[i], sFaceTexts[i]);
		}
        for (int i = 0; i < sSmileyTexts.length; i++) {
            smileyToRes.put(sSmileyTexts[i], sIconIds[i]);
            resToSmiley.put(sIconIds[i], sSmileyTexts[i]);
        }

		mSmileyToRes = smileyToRes;
		mResToSmiley = resToSmiley;

//		smileyToRes = new HashMap<String, Integer>(sOldSmileyTexts.length);
//		for (int i = 0; i < sOldSmileyTexts.length; i++) {
//			String temp = sOldSmileyTexts[i];
//			if(temp.length() > 0){
//				smileyToRes.put(temp, getIconIds()[i]);
//			}
//		}
//
//		mOldSmileyToRes = smileyToRes;
	}

    public static int[] getSendIconsIds() {
        return sFaceIds;
    }

	public static int getSendCount() {
		return sFaceIds.length;
	}

	public String getSmileyByRes(int resId) {
		return mResToSmiley.get(resId);
	}

}